import 'dart:ui' as ui;
import 'package:flame/components/component.dart';
import 'package:flame/components/composed_component.dart';
import 'package:flame/components/mixins/has_game_ref.dart';
import 'package:flame/components/mixins/resizable.dart';
import 'package:flame/components/mixins/tapable.dart';
import 'package:flame/flame.dart';
import 'package:flame/game.dart';
import 'package:flame/gestures.dart';
import 'package:flutter/material.dart';
import 'package:fluttergame/sprite/cloud.dart';
import 'package:fluttergame/sprite/dino.dart';
import 'package:fluttergame/sprite/horizon.dart';
import 'package:fluttergame/sprite/obstacle.dart';

class MyGame extends BaseGame with TapDetector, HasWidgetsOverlay {

  final minSpeed = 6.5;
  final maxSpeed = 13.0;
  final acceleration = 0.001;
  double currentSpeed;

  GameBg gameBg;
  Horizon horizon;
  Cloud cloud;
  Obstacle obstacle;
  Dino dino;
  ui.Image spriteImage;

  MyGame(this.spriteImage){
    currentSpeed = minSpeed;
    print(currentSpeed);
  }

  @override
  void resize(ui.Size size) {
    if(components.isEmpty){
      gameBg = GameBg(Color.fromRGBO(245, 243, 245, 1));
      horizon = Horizon(spriteImage, size);
      cloud = Cloud(spriteImage, size);
      obstacle = Obstacle(spriteImage, size);
      dino = Dino(spriteImage, size);
      this
        ..add(gameBg)..add(horizon)..add(cloud)..add(dino)..add(obstacle)
        ..addWidgetOverlay('upButton', createButton(
          icon: Icons.arrow_drop_up,
          right: 50,
          bottom: 120,
          onHighlightChanged: (isOn)=>dino?.jump(isOn),
        ))
        ..addWidgetOverlay('downButton', createButton(
          icon: Icons.arrow_drop_down,
          right: 50,
          bottom: 50,
          onHighlightChanged: (isOn)=>dino?.down(isOn),
        ));
    }
    super.resize(size);
  }

  Widget createButton({@required IconData icon, double right=0, double
  bottom=0,
  ValueChanged<bool>
  onHighlightChanged}){
    return Positioned(
      right: right,
      bottom: bottom,
      child: MaterialButton(
        onHighlightChanged: onHighlightChanged,
        onPressed: (){},
        splashColor: Colors.transparent,
        highlightColor: Colors.transparent,
        child: Container(
          width: 50,
          height: 50,
          decoration: new BoxDecoration(
            color: Color.fromRGBO(0, 0, 0, 0.5),
            //设置四周圆角 角度
            borderRadius: BorderRadius.all(Radius.circular(50)),
            //设置四周边框
            border: new Border.all(width: 2, color: Colors.black),
          ),
          child: Icon(icon, color: Colors.black,),
        ),
      ),
    );
  }

  void onTap(){
    if(size == null)return;
    dino.startPlay();
  }

  @override
  void update(double t) {
    if(size == null)return;
    if(currentSpeed < maxSpeed){
      currentSpeed += acceleration;
    }
    if(dino.isPlay){
      horizon.updateWithSpeed(t, currentSpeed);
      cloud.updateWithSpeed(t, currentSpeed);
      obstacle.updateWithSpeed(t, currentSpeed);
    }
    dino.updateWithSpeed(t, currentSpeed);
  }

}

class GameBg extends Component with Resizable {
  Color bgColor;
  GameBg([this.bgColor = Colors.white]);

  @override
  void render(Canvas canvas) {
    Rect bgRect = Rect.fromLTWH(0, 0, size.width, size.height);
    Paint bgPaint = Paint();
    bgPaint.color = bgColor;
    canvas.drawRect(bgRect, bgPaint);
  }

  @override
  void update(double t) {}

}